package com.notepad.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.notepad.R;
import com.notepad.adapter.CalendarAdapter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

public class CalendarActivity extends Activity implements OnGestureListener {
	private String current_date = "";
	private int cur_year;
	private int cur_month;
	private int cur_date;

	private GestureDetector gestureDetector;
	private CalendarAdapter mCalendarAdapter;
	private GridView gridView = null;
	private TextView topText = null;
	private static int jumpMonth = 0; // 每次滑动，增加或减去一个月,默认为0（即显示当前月）
	private static int jumpYear = 0; // 滑动跨越一年，则增加或者减去一年,默认为0(即当前年)

	private ImageButton mBackButton;

	public CalendarActivity() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
		current_date = sdf.format(date);
		cur_year = Integer.parseInt(current_date.split("-")[0]);
		cur_month = Integer.parseInt(current_date.split("-")[1]);
		cur_date = Integer.parseInt(current_date.split("-")[2]);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.calendar);
		
		gestureDetector = new GestureDetector(this);

		mCalendarAdapter = new CalendarAdapter(this, getResources(), jumpMonth, jumpYear,
				cur_year,cur_month, cur_date);
		gridView = (GridView) findViewById(R.id.gridview);
		addGridView();
		gridView.setAdapter(mCalendarAdapter);

		topText = (TextView) findViewById(R.id.tv_month);
		addTextToTopTextView(topText);
		
		mBackButton = (ImageButton) findViewById(R.id.btn_goback);
		mBackButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
	}

	// 添加头部的年 月信息
	public void addTextToTopTextView(TextView view) {
		StringBuffer textDate = new StringBuffer();
		textDate.append(mCalendarAdapter.getShowYear()).append("年")
		.append(mCalendarAdapter.getShowMonth()).append("月").append("\t");
		view.setText(textDate);
		view.setTextColor(Color.WHITE);
		view.setTypeface(Typeface.DEFAULT_BOLD);
	}

	private void addGridView() {
		gridView.setOnTouchListener(new OnTouchListener() {
			// 把gridview中的触摸事件回传给gestureDetector
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return CalendarActivity.this.gestureDetector
						.onTouchEvent(event);
			}
		});
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}


	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		
		if (e1.getX() - e2.getX() > 120) {
			// 像左滑动
			addGridView(); // 添加一个gridView
			jumpMonth++; // 下一个月

			mCalendarAdapter = new CalendarAdapter(this, getResources(), jumpMonth,
					jumpYear, cur_year, cur_month, cur_date);
			gridView.setAdapter(mCalendarAdapter);
			addTextToTopTextView(topText);

			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			// 向右滑动
			addGridView(); // 添加一个gridView
			jumpMonth--; // 上一个月

			mCalendarAdapter = new CalendarAdapter(this, getResources(), jumpMonth,
					jumpYear, cur_year, cur_month, cur_date);
			gridView.setAdapter(mCalendarAdapter);
			addTextToTopTextView(topText);

			return true;
		}
		return false;

	}



}
